#ifndef SCRATCH_DETECTION_H
#define SCRATCH_DETECTION_H
#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string>

#include <pcl/ModelCoefficients.h>
#include <pcl/common/transforms.h>
#include <pcl/console/parse.h>
#include <pcl/features/normal_3d.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/passthrough.h>
#include <pcl/filters/project_inliers.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/surface/concave_hull.h>
#include <pcl/surface/convex_hull.h>

#include <Eigen/Core>
#include <Eigen/Geometry>
using namespace std;

class ScratchDetection{
public:
    typedef pcl::PointXYZ PointT;
    typedef pcl::PointCloud<PointT> PointCloudT;
    ScratchDetection();
    ~ScratchDetection();
    //设置输入点云
    void setInputCloud(PointCloudT::Ptr &cloud_in);
    //开始检测
    int compute();
    //划痕检测结果
    float scratch_depth = 0;
    float scratch_width = 0;
    float scratch_length = 0;
    //划痕点云
    PointCloudT::Ptr scratch_cloud;
private:
    //输入点云
    PointCloudT::Ptr cloud_src;
    //计算平面到xoy面，即z=0平面的变换矩阵
    void Trans2Plane(pcl::ModelCoefficients::Ptr &plan_coeff,
                     Eigen::Affine3f &trans_matrix);
    //计算直线到y轴方向，即(0,1,0)的变换矩阵
    void Trans2line(pcl::ModelCoefficients::Ptr &line_coeff,
                    Eigen::Affine3f &trans_matrix);
    //增大z坐标，扩大深度差异，便于检测划痕
    void IncreaseZ(PointCloudT::Ptr &cloud_in, PointCloudT::Ptr &cloud_out,
                   float increaseK);
    //计算深度
    float compute_depth(PointCloudT::Ptr &cloud_in);
    //计算宽度
    float compute_width(PointCloudT::Ptr &cloud_in);
    //计算长度
    float compute_length(PointCloudT::Ptr &cloud_in);

    //将点云投影到z=0平面
    void proj2z0(PointCloudT::Ptr &cloud_in, PointCloudT::Ptr &cloud_out,float zlimit);
};


#endif // SCRATCH_DETECTION_H
